package com.sunther.document.entity;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;

import java.util.List;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/22 10:48
 */
public class RoleByTagEntityVo {
    private Integer id;

    private String name;

    private String title;

    private Integer tagId;

    private Integer parentId;

    private Integer sort;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getParentId() {
        return parentId;
    }

    public Integer getTagId() {
        return tagId;
    }

    public void setTagId(Integer tagId) {
        this.tagId = tagId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }


    public static List<Tree<String>> getTrees(List<RoleByTagEntityVo> roleTagList) {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //默认为id
        treeNodeConfig.setIdKey("tagId");
        //默认为parentId
        treeNodeConfig.setParentIdKey("parentId");
        //排序字段
        treeNodeConfig.setWeightKey("sort");
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        //2.转为树结构,其中rootId参数代表根节点的父级id值
        List<Tree<String>> treeList = TreeUtil.build(roleTagList, "0", treeNodeConfig,
                (treeNode, tree)-> {
                    //对key进行映射赋值
                    tree.setId(String.valueOf(treeNode.getTagId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSort());
                    tree.setName(treeNode.getTitle());
                });
        return treeList;
    }
}
